// firstPackage/pages/reportModule/reportRecord/reportRecord.js

import { queryRecommendList } from "../../../../service/custom_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    selectIndex: 0,//顶部选中
    switchArr: [
      //key为报备状态
      {
        title: "报备",
        key: 2
      },
      {
        title: "到访",
        key: 3
      },
      {
        title: "认购",
        key: 4,
      },
      {
        title: "签约",
        key: 5
      },
      {
        title: "失效",
        key: -1
      }

    ],
    recordList: [

    ],
    //查询条件
    page: 0,
    pageSize: 10,
    recordStatus: 2,
    emptyDesTitle: "暂无报备数据",
    scrollRefreshStatus: true,
    isShowLoading: false,
    isBottomShow: false,

    //状态章:
    noSubmitIcon: "https://saas.img.ylyc.top/icon/zhangwtj_icon.png",//未提交
    failIcon: "https://yf.ylyc.top/icon/yfbbfail_icon.png",//报备失败
    reportProgressIcon: "https://yf.ylyc.top/icon/yfbbprogreess_icon.png",//报备中
    successRecordIcon: "https://yf.ylyc.top/icon/yfybbb_icon.png",//报备成功
    shSuccessIcon: "https://saas.img.ylyc.top/icon/zhangshcgg_icon.png",//审核成功
    shFailIcon: "https://saas.img.ylyc.top/icon/zhangshsb_icon.png",//审核失败
    shProgressIcon: "https://saas.img.ylyc.top/icon/zhangshzz_icon.png",//审核中
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.status) {
      this.setData({
        recordStatus: options.status
      })
    }
    if (this.data.recordStatus == 3) {
      this.setData({
        selectIndex: 1,
        emptyDesTitle: "暂无到访数据"
      })
    }
    if (this.data.recordStatus == 4) {
      this.setData({
        selectIndex: 2,
        emptyDesTitle: "暂无认购数据"
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryRecommendList();
  },


  //顶部切换
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let key = e.currentTarget.dataset.key;
    this.setData({
      selectIndex: index,
      recordStatus: key,
      page: 0,
      recordList: [],
      isBottomShow: false
    })
    this.emptyDesTitleAction(index);

    this.queryRecommendList();
  },
  emptyDesTitleAction(index) {
    switch (index) {
      case 0:
        this.setData({
          emptyDesTitle: "暂无报备数据"
        })
        break;
      case 1:
        this.setData({
          emptyDesTitle: "暂无到访数据"
        })
        break;
      case 2:
        this.setData({
          emptyDesTitle: "暂无认购数据"
        })
        break;
      case 3:
        this.setData({
          emptyDesTitle: "暂无签约数据"
        })
        break;
      case 4:
        this.setData({
          emptyDesTitle: "暂无无效数据"
        })
        break;
      default:
        break;
    }
  },
  //查询列表
  queryRecommendList() {
    wx.showLoading();
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    queryRecommendList(this.data.page, this.data.pageSize, this.data.recordStatus).then(res => {
      this.setData({
        scrollRefreshStatus: false,
      })
      wx.hideLoading();
      console.log("JJLog:报备列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        let tempArr = [];
        tempArr = this.data.recordList;
        if (tempArr.length > 0 && pageNum > 0) {
          //加载下一页:
          tempArr = tempArr.concat(res.data);
          if (res.data.length < pageSize) {
            this.setData({
              isShowLoading: false,
              isBottomShow: true,
            })
            this.setData({
              recordList: tempArr
            })
          } else if (res.data.length == pageSize) {
            this.setData({
              isShowLoading: false,
            })
            this.setData({
              recordList: tempArr
            })
          }
        } else {
          this.setData({
            isShowLoading: false,
          })
          this.setData({
            recordList: res.data
          })
          if (res.data.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
      }
    }).catch(err => {
      console.error("JJLog:queryRecommendList:", err);
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false,
      })
      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },


  //跳转到报备详情:
  pushToRecordDetail(e) {
    let item = e.currentTarget.dataset.item;
    if (item.status == 2 && !item.isReportDeveloper) {
      wx.showToast({
        title: '必须要报备开发商以后，才能进行带看',
        icon: "none"
      })
      return;
    }
    wx.navigateTo({
      url: '/firstPackage/pages/reportModule/reportRecordDetail/reportRecordDetail?id=' + item.id,
    })
  },

  //到底了刷新:
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.page + 1;
    this.setData({
      page: tempPage,
      isShowLoading: true,
    })
    this.queryRecommendList();
  },
  //下拉刷新:
  refreshDownData() {
    console.log("JJLog:下拉刷新来了=====:");
    this.setData({
      scrollRefreshStatus: true,
      page: 0,
      isBottomShow: false,
    })
    this.queryRecommendList();
  },




  //预览确客码:
  previewQrcode(e) {
    let url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url,
      urls: [url],
    })
  },

  // 复制报备信息操作
  copyInfoAction(e) {
    let item = e.currentTarget.dataset.item;
    console.log("JJLog:复制的内容是:" + JSON.stringify(item));
    let copyText = "";
    let companyName = item.companyName ? item.companyName : '自由经纪人';
    copyText = "报备项目：" + item.houseName + "\n";//楼盘名称
    copyText = copyText + '报备公司：' + companyName + "\n";//物业类型
    copyText = copyText + '报备人姓名：' + item.recommendName + '\n';
    copyText = copyText + '报备人电话：' + item.recommendPhone + '\n';
    copyText = copyText + '客户姓名：' + item.customerName + "\n";//区域
    copyText = copyText + '客户电话：' + item.customerPhone + "\n";//参考价
    copyText = copyText + '预计到访时间：' + item.visitingTimeAt + '\n';

    wx.setClipboardData({
      data: copyText,
      success: (res) => {
        wx.getClipboardData({
          success: (res) => {
            wx.showToast({
              title: '复制信息成功'
            })
          },
          fail: (err) => {
            wx.showToast({
              title: '复制失败',
              icon: "none"
            })
          }
        })
      }
    })
  },



})